package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.SysDeptBo;
import com.ruoyi.zhgd.domain.vo.SysDeptVo;
import com.ruoyi.zhgd.domain.SysDeptBean;
import com.ruoyi.zhgd.mapper.SysDeptBeanMapper;
import com.ruoyi.zhgd.service.ISysDeptBeanService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 部门Service业务层处理
 *
 * @author zyb
 * @date 2023-09-13
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class SysDeptBeanBeanServiceImpl implements ISysDeptBeanService {

    private final SysDeptBeanMapper baseMapper;

    /**
     * 查询部门
     */
    @Override
    public SysDeptVo queryById(Long deptId){
        return baseMapper.selectVoById(deptId);
    }

    /**
     * 查询部门列表
     */
    @Override
    public TableDataInfo<SysDeptVo> queryPageList(SysDeptBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysDeptBean> lqw = buildQueryWrapper(bo);
        Page<SysDeptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询部门列表
     */
    @Override
    public List<SysDeptVo> queryList(SysDeptBo bo) {
        LambdaQueryWrapper<SysDeptBean> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysDeptBean> buildQueryWrapper(SysDeptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysDeptBean> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, SysDeptBean::getDeptId, bo.getDeptId());
        lqw.eq(bo.getParentId() != null, SysDeptBean::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getAncestors()), SysDeptBean::getAncestors, bo.getAncestors());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), SysDeptBean::getDeptName, bo.getDeptName());
        lqw.eq(bo.getOrderNum() != null, SysDeptBean::getOrderNum, bo.getOrderNum());
        lqw.eq(StringUtils.isNotBlank(bo.getLeader()), SysDeptBean::getLeader, bo.getLeader());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), SysDeptBean::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), SysDeptBean::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysDeptBean::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPkid()), SysDeptBean::getPkid, bo.getPkid());
        lqw.eq(StringUtils.isNotBlank(bo.getPid()), SysDeptBean::getPid, bo.getPid());
        lqw.eq(StringUtils.isNotBlank(bo.getPids()), SysDeptBean::getPids, bo.getPids());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysDeptBean::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysDeptBean::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getSort()), SysDeptBean::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getSpell()), SysDeptBean::getSpell, bo.getSpell());
        lqw.eq(bo.getHasLeaf() != null, SysDeptBean::getHasLeaf, bo.getHasLeaf());
        lqw.eq(StringUtils.isNotBlank(bo.getState()), SysDeptBean::getState, bo.getState());
        lqw.eq(bo.getLongitude() != null, SysDeptBean::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLatitude() != null, SysDeptBean::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), SysDeptBean::getInfo, bo.getInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), SysDeptBean::getAddress, bo.getAddress());
        lqw.eq(bo.getCreator() != null, SysDeptBean::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, SysDeptBean::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, SysDeptBean::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, SysDeptBean::getUpdateDate, bo.getUpdateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), SysDeptBean::getCode, bo.getCode());
        lqw.eq(bo.getSerial() != null, SysDeptBean::getSerial, bo.getSerial());
        lqw.eq(StringUtils.isNotBlank(bo.getPerson()), SysDeptBean::getPerson, bo.getPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonTel()), SysDeptBean::getPersonTel, bo.getPersonTel());
        lqw.eq(bo.getStartTime() != null, SysDeptBean::getStartTime, bo.getStartTime());
        return lqw;
    }

    /**
     * 新增部门
     */
    @Override
    public Boolean insertByBo(SysDeptBo bo) {
        SysDeptBean add = BeanUtil.toBean(bo, SysDeptBean.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDeptId(add.getDeptId());
        }
        return flag;
    }

    /**
     * 修改部门
     */
    @Override
    public Boolean updateByBo(SysDeptBo bo) {
        SysDeptBean update = BeanUtil.toBean(bo, SysDeptBean.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysDeptBean entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除部门
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
